<?php
include './php-ofc-library/open-flash-chart.php';

$link=mysql_connect ("localhost","sun","") or die ("Can not connect to db!".mysql_error());
mysql_select_db("jcat",$link) or die ("Can not select db!".mysql_error());


$Params = $_GET['Params'];
list($chart, $trader, $traderNo, $specialist, $level1, $level2, $game, $items, $start, $status) = split('[,]', $Params);

$Experiment = $level1.'_'.$level2.'_'.$game;

$items =(int)$items;
$step = 1;
if($items>20) $step = floor($items/20);

$accumulating = (int)$start+1;
$final = (int)$start + (int)$items -1;
$xLables = array();
while ( $accumulating <= ($items+(int)$start+1) )
{
	$xLables[] = $accumulating;
	$accumulating=$accumulating+$step;
}

if($specialist!=''){
	$querySpecialist = " AND SpecialistId = '".$specialist."' ";
}else{
	$querySpecialist = "";
}

	$sql="SELECT `score.marketshare`, `score.profit`, `score.transactionrate`, `efficiency`, `convergence_coefficient` FROM x_score WHERE ExperimentId = '".$Experiment."' ".$querySpecialist." ORDER BY day ASC LIMIT ".$start.",".$items.";";

	$result = mysql_query($sql,$link) or die ("Can not read db!".mysql_error());

	$efficiency = array();
	$convergenceCoefficient = array();

	$i=0;
	while($row = mysql_fetch_array($result, MYSQL_ASSOC))
	{
		$marketshare[$i] = round($row['score.marketshare'],6)*100;
		$profit[$i] = round($row['score.profit'],6)*100;
		$transactionRate[$i] = round($row['score.transactionrate'],6)*100;
		$efficiency[$i] = round($row['efficiency'],3);
		$convergenceCoefficient[$i] = round($row['convergence_coefficient'],3);
		$i++;		
	} 

$line_marketshare = new line_dot();
$line_marketshare->set_width( 1 );
$line_marketshare->set_colour( '#CA5920' );
$line_marketshare->set_dot_size( 3 );
$line_marketshare->set_values( $marketshare );
$line_marketshare->set_tooltip( 'Marketshare: #val#' );
$line_marketshare->set_key( "Marketshare", 10 );

$line_profit = new line_dot();
$line_profit->set_width( 1 );
$line_profit->set_colour( '#218036' );
$line_profit->set_dot_size( 3 );
$line_profit->set_values( $profit );
$line_profit->set_tooltip( 'Profit: #val#' );
$line_profit->set_key( "Profit", 10 );

$line_transactionRate = new line_dot();
$line_transactionRate->set_width( 1 );
$line_transactionRate->set_colour( '#0097C8' );
$line_transactionRate->set_dot_size( 3 );
$line_transactionRate->set_values( $transactionRate );
$line_transactionRate->set_tooltip( 'Transaction Rate: #val#' );
$line_transactionRate->set_key( "Transaction Rate", 10 );
	
$line_efficiency = new line_hollow();
$line_efficiency->set_width( 1 );
$line_efficiency->set_colour( '#5EA725' );
$line_efficiency->set_dot_size( 3 );
$line_efficiency->set_values( $efficiency );
$line_efficiency->set_tooltip( 'Efficiency: #val#' );
$line_efficiency->set_key( "Efficiency", 10 );

$line_convergenceCoefficient = new line_hollow();
$line_convergenceCoefficient->set_width( 1 );
$line_convergenceCoefficient->set_colour( '#AEA725' );
$line_convergenceCoefficient->set_dot_size( 3 );
$line_convergenceCoefficient->set_values( $convergenceCoefficient );
$line_convergenceCoefficient->set_tooltip( 'ConvergenceCoefficient: #val#' );
$line_convergenceCoefficient->set_key( "ConvergenceCoefficient", 10 );

$chart = new open_flash_chart();

$chart->set_title( new title( $Experiment.' - Coefficient  -  '.$specialist.' ('.$start.' - '.$final.')' ) );

$x_legend = new x_legend( 'Day' );
$x_legend->set_style( '{font-size: 12px; color: #333333}' );
$chart->set_x_legend( $x_legend );

$y_legend = new y_legend( 'Percentage' );
$y_legend->set_style( '{font-size: 12px; color: #333333}' );
$chart->set_y_legend( $y_legend );

$x_labels = new x_axis_labels();
$x_labels->set_steps( $step );
//$x_labels->set_vertical();
$x_labels->set_colour( '#000000' );
//$x_labels->set_labels( $items );

$x = new x_axis();
$x->set_colour( '#333333' );
//$x->set_grid_colour( '#D7E4A3' );
$x->set_offset( false );
$x->set_steps( $step );
$x->set_range( $start, $final+1 );

$chart->set_x_axis( $x );

$y = new y_axis();
$chart->set_y_axis( $y );
$y->set_range( 0, 100, 10 ); 

//
// here we add our data sets to the chart:
//
$chart->add_element( $line_marketshare );
$chart->add_element( $line_profit );
$chart->add_element( $line_transactionRate );
$chart->add_element( $line_efficiency );
$chart->add_element( $line_convergenceCoefficient );

echo $chart->toPrettyString();

?>
